import 'babel-polyfill'
import React from 'react'
import {render} from 'react-dom'
import {BrowserRouter} from 'react-router-dom'
import 'antd-mobile/dist/antd-mobile.css'
import './common/font/iconfont.css'
import './common/style/index.scss'
import App from './app'
import {createStore} from 'redux'
import {Provider} from 'react-redux'
import reducers from './reducers'
import {chatWebSocket} from './websocket'
import FastClick from 'fastclick'
import {post} from './common/js/utils'
import {setUserInfo} from './actions'
import Login from './views/login'

FastClick.attach(document.body)

const store = createStore(reducers)
chatWebSocket(store)

const rootElement = document.getElementById('root')

post('/users/check').then(res => {
  const o = res.data || {}
  let node
  if (res.result === 1) {
    Object.assign(o, {status: 1})
    node = (
      <BrowserRouter>
        <Provider store={store}>
          <App store={store} />
        </Provider>
      </BrowserRouter>
    )
  } else {
    Object.assign(o, {status: -1})
    node = (<Login />)
  }
  store.dispatch(setUserInfo(o))
  render(node, rootElement)
}).catch((err) => {
  render(
    <div
      style={{
        padding: '10px 0',
        textAlign: 'center'
      }}
    >{err.message || '未知错误'}</div>
  )
})
